<?php
/**
 * Created by PhpStorm.
 * User: 我型我素
 * Date: 2018/3/15
 * Time: 10:26
 */

namespace SDXApi\Controller;


use Think\Controller;
use SDXApi\Service\Wechat;

class MiniController extends Controller
{
    private $AppID;
    private $AppSecret;
    private $weObj;
    private $options = array();

    protected function _initialize(){
        $this->AppID = C('wechat_app_app_id');
        $this->AppSecret = C('wechat_app_secret');

        $this->options['appid'] = $this->AppID;
        $this->options['appsecret'] = $this->AppSecret;

        $this->weObj = new Wechat($this->options);
    }

    public function getSharePicUrl(){
        //定义输出格式为image
        header('Content-type: image/jpg');

        $page = I('page');

        $param = array();
        $param['scene'] = I('scene');
        $param['page'] = urldecode($page);
        $param['width'] = I('width');
        $param['auto_color'] = true;

        //获取小程序码图片二进制流
        $pic_res = $this->getWxaCode($param);

        //转换成图片
        $image = imagecreatefromstring($pic_res);

        //输出图片
        echo imagejpeg($image,null,100);
    }

    public function getWxaCode($param){
        $param['auto_color'] = true;
        $res = $this->weObj->getWxacodeUnlimit($param);

        return $res;
    }
}